package cn.hznc.controller.service.toollife.logic;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.controller.service.process.logic.ProcessBomDo;
import cn.hznc.domain.entity.ProcessBomEntity;
import cn.hznc.domain.entity.ToolLifeEntity;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author：Administrator
 * @Date：2023/6/3 14:52
 */
public class ToolLifeLogic{
    private final ToolLifeDo toolLifeDo;

    private final List<ToolLifeEntity> originToolLifeList;

    private final List<ToolLifeEntity> updateToolLifeList;

    public ToolLifeLogic(List<ToolLifeEntity> originToolLifeList, List<ToolLifeEntity> updateToolLifeList) {
        this.originToolLifeList = originToolLifeList;
        this.updateToolLifeList = updateToolLifeList;
        this.toolLifeDo=new ToolLifeDo();
    }

    /**
     * 获取处理后的业务模型
     *
     * @return subProgramDo
     */
    public ToolLifeDo getToolLifeDo() {
        return toolLifeDo;
    }

    /**
     * 分离增删改数据
     */
    public void buildToolLifeDo(){
        if (CollectionUtil.isEmpty(originToolLifeList)){
            toolLifeDo.addCreateElement(originToolLifeList);
            return;
        }
        if (CollectionUtil.isEmpty(updateToolLifeList)){
            toolLifeDo.addDeleteElement(updateToolLifeList);
            return;
        }
        // 比对id判断，对删除，修改，新增
        List<Integer> ids = originToolLifeList.stream().map(ToolLifeEntity::getId).collect(Collectors.toList());
        List<Integer> updateIds = updateToolLifeList.stream().map(ToolLifeEntity::getId).filter(ObjectUtil::isNotNull).collect(Collectors.toList());
        for (ToolLifeEntity toolLifeEntity : updateToolLifeList) {
            // 添加修改元素
            if (ids.contains(toolLifeEntity.getId())) {
                toolLifeDo.addUpdateElement(toolLifeEntity);
            }
            // 添加新增元素
            if (ObjectUtils.isEmpty(toolLifeEntity.getId())){
                toolLifeDo.addCreateElement(toolLifeEntity);
            }
        }
        for (ToolLifeEntity toolLifeEntity : originToolLifeList) {
            // 添加删除元素
            if (!updateIds.contains(toolLifeEntity.getId())){
                toolLifeDo.addDeleteElement(toolLifeEntity);
            }
        }
    }
}
